package jsyf.oa.dao.weekplan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.weekplan.TomorrowVisitPlanEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LT on 2018/3/22.
 */
@Repository
public class TomorrowVisitDao extends BaseDao {
    public TomorrowVisitDao() {
        super.iBaseEntity = new TomorrowVisitPlanEntity();
    }

    public List<JSONObject> getTomorrowVisitToday(String userId, String dateTime) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT\n" +
                "\tVISIT_GOAL AS visitGoal,\n" +
                "\tREMAKE AS remake,\n" +
                "\tW_ID AS wId,\n" +
                "\tCUSTOMER_ADDRESS AS customerAddress,\n" +
                "\tCUSTOMER_INDUSTRY AS customerIndustry,\n" +
                "\tVISIT_METHOD AS visitMethod,\n" +
                "\tENTOURAGE AS entourage,\n" +
                "\tCUSTOMER_NAME AS customerName,\n" +
                "\tINTENTION_DEVICE AS intentionDevice,\n" +
                "\tID AS id,\n" +
                "\tCREATE_USER_ID AS createUserId,\n" +
                "\tCREATE_TIME AS createTime,\n" +
                "\tLAST_VISIT_TIME AS lastVisitTime,\n" +
                "\tREPORT_DATE AS reportDate\n" +
                " FROM\n" +
                "\tT_WRT_CUSTOMER_VIST_PLAN\n" +
                "WHERE\n" +
                "\t1 = 1\n" +
                "AND CREATE_USER_ID ='" + userId + "'\n" +
                "AND REPORT_DATE ='" + dateTime + "'\n");
        return super.queryForJsonList(sql.toString());
    }

    public Integer getPageCount(String userId,String dateTime) {
        StringBuffer sql = new StringBuffer();
        sql.append("  SELECT\n" +
                "\tCOUNT(1) AS paln\n" +
                "FROM\n" +
                "\tT_WRT_CUSTOMER_VIST_PLAN\n" +
                "WHERE\n" +
                "\t1 = 1\n" +
                "AND CREATE_USER_ID ='"+userId+"'\n" +
                "AND REPORT_DATE ='"+dateTime+"'\n");
        String count = super.queryForString(sql.toString());
        return Integer.parseInt(count);
    }

    public List<TomorrowVisitPlanEntity> getByCondition(String condition, Object[] params) {
        return this.getByPage(condition, params, 1, Integer.MAX_VALUE);
    }

    public List<TomorrowVisitPlanEntity> getByPage(String condition, Object[] params, int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" ID AS id, \n");
        sql.append(" CUSTOMER_NAME AS customerName, \n");
        sql.append(" COUNTY AS county, \n");
        sql.append(" LAST_VISIT_TIME AS lastVisitTime, \n");
        sql.append(" CUSTOMER_ADDRESS AS customerAddress, \n");
        sql.append(" INTENTION_DEVICE AS intentionDevice, \n");
        sql.append(" VISIT_METHOD AS visitMethod, \n");
        sql.append(" VISIT_GOAL AS visitGoal, \n");
        sql.append(" ENTOURAGE AS entourage, \n");
        sql.append(" REMAKE AS remake, \n");
        sql.append(" W_ID AS wId, \n");
        sql.append(" CREATE_USER_ID AS createUserId, \n");
        sql.append(" CREATE_TIME AS createTime, \n");
        sql.append(" CUSTOMER_INDUSTRY AS customerIndustry \n");
        sql.append(" FROM T_WRT_CUSTOMER_VIST_PLAN \n");
        sql.append(" WHERE 1 = 1 \n");
        sql.append(condition);
        sql.append("  ORDER BY CUSTOMER_NAME IS NOT NULL AND CUSTOMER_NAME != '' DESC, CREATE_TIME");
        super.appendPageSql(sql, requestPage, pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);

        List<TomorrowVisitPlanEntity> list = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            TomorrowVisitPlanEntity model = JSON.toJavaObject(jsonList.get(i), TomorrowVisitPlanEntity.class);
            list.add(model);
        }
        return list;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, TomorrowVisitPlanEntity.class);
    }
}
